package com.pai4j.pgc.controller.community;

import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.config.annotation.RequestPermission;
import com.pai4j.common.enums.CommonStatusEnum;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.community.vo.request.CCNavControlRequestVO;
import com.pai4j.domain.community.vo.request.CCNavRequestVO;
import com.pai4j.domain.community.vo.response.cc.CTCNavPathNavListResponseVO;
import com.pai4j.domain.community.vo.response.cc.CTCNavResponseVO;
import com.pai4j.pgc.service.community.CCNavCoreCacheService;
import com.pai4j.pgc.service.community.CCNavService;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 技术社区导航
 *
 * @author: CYM-pai
 * @date: 2025/10/26 21:54
 **/
@RestController
@RequestMapping("/api/cc/nav")
public class CCNavController {

    @Autowired
    private CCNavService navService;

    @Autowired
    private CCNavCoreCacheService navCacheService;

    /***
     * 保存导航
     * @param vo
     * @return
     */
    @RequestPermission
    @PostMapping("/save")
    public PAIResponseBean<Long> save(@Valid @RequestBody CCNavRequestVO vo) {
        return PAIResponseBeanUtil.success(navService.save(vo, SessionHelper.getCurrentUserId()));
    }

    /***
     * 修改状态
     * @param vo
     * @return
     */
    @RequestPermission
    @PostMapping("/updateStatus")
    public PAIResponseBean updateStatus(@RequestBody CCNavControlRequestVO vo) {
        navService.updateStatus(vo, SessionHelper.getCurrentUserId());
        return PAIResponseBeanUtil.success();
    }

    /**
     * 用户端导航查询
     * @param ccId
     * @return
     */
    @GetMapping("/list/{ccId}")
    public PAIResponseBean<List<CTCNavPathNavListResponseVO>> navList(@PathVariable Long ccId) {

        return PAIResponseBeanUtil.success(navCacheService.getCoreNavFromCache(ccId));
    }

    /***
     * 列表管理
     * @return
     */
    @RequestPermission
    @GetMapping("/list")
    public PAIPageResponseBeanUtil<List<CTCNavResponseVO>> list(@RequestParam(value = "pathId", required = false) Long pathId,
                                                                @RequestParam(value = "keyword", required = false) String keyword,
                                                                @RequestParam(value = "status", required = false) String status,
                                                                @RequestParam(value = "pageNo", required = false,
                                                                 defaultValue = "1") Integer pageNo,
                                                                @RequestParam(value = "pageSize", required = false,
                                                                 defaultValue = "10") Integer pageSize) {
        List<String> statusList = new ArrayList<>();
        if (StringUtils.isBlank(status)) {
            statusList = CommonStatusEnum.getAllEnableStatusDesc();
        } else {
            statusList.add(status);
        }
        return navService.list(pathId, keyword, statusList, pageNo, pageSize, null);
    }
}
